package oop.data;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import oop.db.dao.SectionQuestionAnswerDAO;

public class Question extends BaseQuestion {

	private long sectionId;
	private double mark;
	private Map<Long, Answer> usedAnswerMap;

	public Question(int id, String content, int level, SimpleCategory category,
			SimpleUser author, long sectionId, double mark) {
		super(id, content, level, category, author);
		this.sectionId = sectionId;
		this.mark = mark;
	}

	public long getSectionId() {
		return sectionId;
	}

	void setSectionId(long sectionId) {
		this.sectionId = sectionId;
	}

	public void setMark(double mark) {
		this.mark = mark;
	}

	public double getMark() {
		return mark;
	}

	public Map<Long, Answer> getUsedAnswerById() throws SQLException {
		if (usedAnswerMap == null) {
			usedAnswerMap = new HashMap<Long, Answer>();
			for (Long id : SectionQuestionAnswerDAO.getUsedAnswerIds(sectionId,
					getId())) {
				usedAnswerMap.put(id, getAnswerById().get(id));
			}
		}
		return usedAnswerMap;
	}

	public void addUsedAnswer(Answer answer) throws SQLException {
		if (!getAnswerById().containsKey(answer.getId())) {
			throw new IllegalArgumentException();
		}
		SectionQuestionAnswerDAO.addAnswer(sectionId, getId(), answer.getId());
		usedAnswerMap.put(answer.getId(), answer);
	}
	
	public void removeUsedAnswer(Answer answer) throws SQLException {
		SectionQuestionAnswerDAO.removeAnswer(sectionId, getId(), answer.getId());
		usedAnswerMap.remove(answer.getId());
	}

}
